-- attrib_damage
-- create by zhuangsl
-- 单体伤害，根据专属技能消耗的属性计算伤害
-- 配置：attrib_damage(slime_skill_id, attrib, force)

return {
    apply = function(source, target, skillId, round, para, extra)
        -- 专属技能等级
        local level;
        extra = extra or {};
        if extra["level"] then
            level = tonumber(extra["level"]);
        else
            local prop = PropM.combine(source, "gain_slime_skill", para[1]);
            level = PropM.apply(prop, 0);
        end

        local attrib = para[2];

        -- 属性总量
        local cost = DungeonServiceM.queryDungeonAttrib(attrib);

        -- 总伤害
        local force = para[3];
        local damage = math.modf(cost * (1000 + force * level) / 1000);

        -- 斯巴达专属加成
        local prop = PropM.combine(source, "battle_cry", 1);
        if prop[3] > 0 then
            -- 触发特技发动事件
            EventMgr.fire(event.SPECIAL_SKILL_TRIGGER, { propId = prop[1] });
        end
        damage = PropM.apply(prop, damage);

        -- 遍历所有的怪
        local ret = {};
        local posList = {};
        local num = 0;

        for p = 1, DUNGEON_WIDTH * DUNGEON_HEIGHT do
            local grid = DungeonM.getGridByPos(p);

            -- 是未死亡的怪物
            if  grid:isOpened() and
                grid:isMonster() and
                not grid.monster:isDead() then
                table.insert(ret, grid.monster);
                table.insert(posList, grid:getPos());
                num = num + 1;
            end
        end

        -- 伤害由所有怪物均摊
        local num = math.max(#ret, 1);
        damage = math.modf(damage / num);
        damage = math.max(damage, 1);

        -- 先执行消耗
        DungeonServiceM.costDungeonAttrib( {[attrib] = cost} );

        -- 使用探索点，特殊处理下
        if attrib == "search_point" then
            -- 抛出特效发动事件
            EventMgr.fire(event.SEARCH_POINT_DAMAGE, { ["pos_list"] = posList, });

            -- 需要UI延迟
            extra["delay_time"] = 0.6 * num;
        end

        -- 对所有目标执行伤害
        for _, monster in pairs(ret) do
            if SkillM.immunitySkill(source, monster, skillId) then
                -- 目标有免疫专属技属性
                if not SkillM.getSequence(monster) then
                    SkillM.initSequence(source, monster, skillId);
                end
                SkillM.getSequence(monster):immunity(source, monster, skillId);
            else
                -- 增加命中动作
                if SkillM.getSequence(monster) == nil then
                    SkillM.getSequenceList()[monster] = CastSequence.create(source, monster, skillId);
                end
                SkillM.getSequence(monster):hit(source, monster, skillId);

                -- 伤害
                CombatM.doDamage(source, monster, damage, skillId, nil, extra);
            end
        end

        -- 作用目标
        return ret;
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { tonumber(arr[1]), arr[2], tonumber(arr[3]), };
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        -- 专属技能等级
        local prop = PropM.combine(source, "gain_slime_skill", para[1]);
        local level = PropM.apply(prop, 0);
        local attrib = para[2];

        -- 属性总量
        local cost = DungeonServiceM.queryDungeonAttrib(attrib);

        -- 总伤害
        local force = para[3];

        local damage = math.modf(cost * (1000 + force * level) / 1000);

        -- 斯巴达专属加成
        prop = PropM.combine(source, "battle_cry", 1);
        damage = PropM.apply(prop, damage);

        desc = string.gsub(desc, "{attrib_damage}", damage);
        desc = string.gsub(desc, "{attrib_cost}", cost);

        return desc;
    end,
};
